//
//  AFNetWorkingHandle.m
//  AFNETWorking
//
//  Created by dllo on 16/1/18.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "AFNetWorkingHandle.h"
#import "AFHTTPRequestOperationManager.h"

@implementation AFNetWorkingHandle

+ (void)GETRequestWithURLString:(NSString *)URLString completionWithSuccess:(void (^)(id result))success withFailure:(void (^)(id error))failure {
    
    // 创建对象
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    // 设置返回的数据类型
    // 返回NSData数据类型
//    manager.responseSerializer = [AFHTTPRequestSerializer serializer];
    // 返回的是JSON数据类型
    // 注意: 默认返回的是json
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    // 设置能接受的返回值的类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    /*
     * @brief get解析
     * @param 1 网址
     * @param 2 字典
     * @param 3 responseObject 解析成功后的数据
     *
     */
    [manager GET:URLString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 解析成功, 调用该block
        success(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 解析出错, 调用该block
        failure(error);
        
    }];
    
}

// post解析方法实现
+ (void)POSTRequestWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters completionWithSuccess:(void (^)(id))success withFailure:(void (^)(id))failure {
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    // 设置请求的格式
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    // 设置返回的数据类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    // 设置能接受的返回值的类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    
    // 参数2: 网络后系数(body)字典形式
    [manager POST:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        // 数据加载成功
        success(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        // 数据加载失败
        failure(error);
        
    }];
    
    
    
    
}




@end
